package SpaceIT.pages.Admin;

import java.util.ArrayList;
import java.util.List;

import model.dao.DongSanPhamDAO;
import model.dao.LoaiSanPhamDAO;
import model.dao.NhaSanXuatDAO;
import model.pojo.DongSanPham;
import model.pojo.HangSanXuat;
import model.pojo.LoaiSanPham;
import model.pojo.NhanVien;
import model.util.IntegerValueEncoder;

import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.internal.SelectModelImpl;

import SpaceIT.pages.Index;

import controller.util.ControllerHelper;

public class ThemDongSanPham {

	@Property
	@Persist
	private String message;
	
	//danh sach hang san xuat
		public List<OptionModel> getAllHangSanXuat()
		{
			ArrayList<HangSanXuat> arr= NhaSanXuatDAO.LayDSHangSanXuat();
			 List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
	        for(int i =0;i<arr.size();i++)
	        {
	        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).getTenHangSX(), new Integer(arr.get(i).getMaHangSX()));  
	        	optionsModel.add(ap);
	        }
	          
	        return optionsModel;  
		}
		public SelectModel getDSHangSanXuat() {  
	        
	        return new SelectModelImpl(null, getAllHangSanXuat());  
	    }  
	  
		//hang san xuat
		@Persist
		@Validate("required")
		private Integer NSX;
		public Integer getNSX() {
			return NSX;
		}
		public void setNSX(Integer nSX) {
			NSX = nSX;
		}
		
		//danh sach loai san pham
		public List<OptionModel> getAllLoaiSP()
		{
			ArrayList<LoaiSanPham> arr = LoaiSanPhamDAO.LayDSLoaiSanPham();
			
			List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
	        for(int i =0;i<arr.size();i++)
	        {
	        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).get_tenLoaiSanPham(), new Integer(arr.get(i).get_maLoai()));  
	        	optionsModel.add(ap);
	        }
	          
	        return optionsModel; 
		}
		public SelectModel getDSLoaiSanPham() {  
	        
	        return new SelectModelImpl(null, getAllLoaiSP());  
	    }  
	  
	      
	    public ValueEncoder<Integer> getValueEncoder(){  
	        return new IntegerValueEncoder();  
	    }  
	      
		//loai san pham
		@Persist
		@Validate("required")
		private Integer LSP;
		
		public Integer  getLSP() {
			return LSP;
		}
		public void setLSP(Integer lSP) {
			LSP = lSP;
		}
		//ten san pham
		@Persist
		@Property
		@Validate("required")
		private String TenSP;
		
		public String EmptyMessage(){
			message = "";
			return "";
		}
		@OnEvent(value="submit", component="formThemDong")
		void onFormSubmit() 
		{	
			try
			{
				DongSanPham dsp = new DongSanPham();
				dsp.setMoTa(ControllerHelper.filter(this.TenSP));
				dsp.setMaHangSX(this.NSX);
				dsp.setMaLoaiSP(this.LSP);
				ArrayList<DongSanPham> arr= DongSanPhamDAO.KiemTraDongSanPham(dsp.getMoTa());
				if(arr.isEmpty()==false)
				{
					message = "Dòng Sản Phẩm Này Đã Tồn tại";
					return;
				}
				boolean kq = DongSanPhamDAO.ThemDongSP(dsp);
				if(kq==true)
				{
					message = "Thêm Dòng Sản Phẩm Thành Công";
				}
				else
					message = "Thêm Dòng Sản Phẩm Thất Bại";
				
			}
			catch (Exception e) {
				// TODO: handle exception
				message="Lỗi Insert Dòng Sản Phẩm";
			}
		}
		@ApplicationState
	    private NhanVien NV;
		public Object onActivate()
		{
			if(NV.isDangNhap()==false)
			{			
				//NV.setDangNhap(false);
				return Index.class;
			}
			return null;
		}
}
